import requests
import sys
import time
import datetime
import random
import threading
import traceback
import json


def time_tag():
    return time.strftime("%Y%m%d%H%M%S", time.localtime())


def time_stamp_ms():
    return str(time_tag() + ' ' + str(datetime.datetime.now().microsecond / 1000))


def random_num(num):
    items = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
    random.shuffle(items)
    return ''.join(items[0:num])


def get_out_trade_no():
    return time_tag() + random_num(4) + '33'


ip_addr = '127.127.127.127'
app_version = '8.8.8'
# host_addr = 'http://119.29.78.41/juhe'
# smit_mch_id = '1128608654'
# mch_id = '1128608654'
host_addr = 'http://pay.smit.com.cn'
smit_mch_id = '1072321057'
mch_id = '1072321057'
# print(get_out_trade_no())
login_url = host_addr + "/do/user/login/fpay"
login_param = {
    # "login_phone": "13812345678",
    # "passwd": "123456"
    "login_phone": "13800000001",
    "passwd": "123456"
}

wxpay_url = host_addr + "/do/fpay/wxpay"


def wxpay_param():
    return {
        "device_info": "20170329",
        "body": "微信条码支付压力测试-" + get_out_trade_no(),
        "detail": "",
        "attach": "",
        "out_trade_no": get_out_trade_no(),
        "total_fee": 1,
        "fee_type": "0",
        "spbill_create_ip": ip_addr,
        "goods_tag": "",
        "limit_pay": "",
        "auth_code": "130128211545058851",
        "smit_mch_id": "1128608654",
        "version": app_version
    }


wx_order_query_url = host_addr + "/do/fpay/orderquery/wx"
wx_order_query_param = {
    "smit_mch_id": smit_mch_id,
    "out_trade_no": "20170328033843940488",
    # "trade_no": "1235468",
}

alipay_url = host_addr + "/do/fpay/alipay"


def alipay_param():
    return {
        "device_info": "20170329",
        "subject": "支付宝条码支付压力测试-" + get_out_trade_no(),
        "detail": "test-detail",
        "attach": "test-attach",
        "out_trade_no": get_out_trade_no(),
        "total_amount": 0.01,
        "fee_type": "0",
        "spbill_create_ip": ip_addr,
        "goods_tag": "test-goods_tag",
        "limit_pay": "test-limit_pay",
        "auth_code": "286731394980269398",
        "smit_mch_id": smit_mch_id,
        "version": app_version
    }


ali_order_query_url = host_addr + "/do/fpay/orderquery/ali"
ali_order_query_param = {
    "smit_mch_id": "1072321057",
    "out_trade_no": "20160823145907098996",
    # "trade_no": "1235468",
}

add_payrecord_url = host_addr + "/do/fpay/payrecord"


def add_payrecord_param():
    return {
        "mchId": mch_id,
        "operaterId": "1",
        "storeId": "1",
        "payTime": datetime.datetime.now(),
        "terminalId": "1",
        "payType": "3",
        "totalFee": random.randrange(10, 1000),
        "customId": "2088002414885093",
        "outTradeNo": get_out_trade_no(),
        "tradeNo": get_out_trade_no(),
        "payStatus": "2",
        "mac": "aa:bb:cc:dd:ee:ff",
        "appVersionName": app_version,
        "ipAddr": ip_addr
    }


def write_log(log):
    filename = time.strftime("%Y%m%d", time.localtime()) + '_log.txt'
    file = open(filename, 'a+')
    file.write(log)
    file.close()


test_count = 0


def test(url, param):
    try:
        rsp = requests.get(url=url, verify=False, params=param)
        # print(test_count)
        # print(rsp)
        # result_json = json.loads(rsp.text)
        # json_str = json.dumps(result_json, sort_keys=True, indent=4, separators=(',', ': '), )
        # print(json_str.encode('utf-8').decode('unicode_escape'))
    except:
        filename = time.strftime("%Y%m%d", time.localtime()) + '_err.txt'
        # file = open(filename, 'w+')
        # traceback.print_exc(file)
        # file.flush()
        # file.close()
        msg = traceback.format_exc()
        write_log(time_stamp_ms() + ' ' + msg)
        print(msg)
        sys.exit(1)


def test_all():
    global test_count
    while True:
        test(login_url, login_param)
        # time.sleep(1)
        test(alipay_url, alipay_param())
        # time.sleep(1)
        test(wxpay_url, wxpay_param())
        # time.sleep(1)
        # test(wx_order_query_url, wx_order_query_param)
        # time.sleep(1)
        # test(ali_order_query_url, ali_order_query_param)
        # time.sleep(1)
        # test(add_payrecord_url, add_payrecord_param())
        test_count += 1
        log = time_tag() + ' ' + str(datetime.datetime.now().microsecond / 1000) + ' total:' + str(
            test_count) + ' thread:' + threading.current_thread().getName()
        print(log)
        write_log(log + '\n')
        # time.sleep(1)


if __name__ == '__main__':
    # test(login_url, login_param)
    # test(alipay_url, alipay_param())
    # test(wxpay_url, wxpay_param())
    for i in range(1, 200):
        t = threading.Thread(target=test_all)
        t.setName(i)
        t.start()
